(function() {

	myAce = {
		customStyle : function() {
			// 个性化显示事件
			$('.right-sidebar-toggle').click(function() {
				$('#right-sidebar').toggleClass('sidebar-open');
			});

			// 个性化滚动条初始化
			$('.sidebar-container').slimScroll({
				height : '100%',
				railOpacity : 0.4,
				wheelStep : 10
			});

			// 绑定全屏事件
			$('.screenfull').click(function() {
				if (screenfull.enabled) {
					if (!screenfull.isFullscreen)
						screenfull.request();
					else
						screenfull.exit();
				}
			});

			// 点击消息后不会隐藏弹出框
			$(".dropdown-menu").on("click", "[data-stopPropagation]", function(e) {
				e.stopPropagation();
			});

			// 简洁菜单
			$('#compactMenu').click(function() {
				setCompactMenu(this.checked);

				// 浮动子菜单
				if (this.checked)
					setHoverSubMenu(this.checked);

				// 取消最小化菜单
				if (this.checked)
					setMinMenu(!this.checked);
			});

			// 最小化菜单
			$('#minMenu').click(function() {
				setMinMenu(this.checked);

				// 取消简洁菜单
				if (this.checked)
					setCompactMenu(!this.checked);
			});

			// 浮动子菜单
			$('#hoverSubMenu').on('click', function() {
				setHoverSubMenu(this.checked);

				// 触发取消简洁菜单
				if (!this.checked) {
					setCompactMenu(this.checked);
				}

				// 触发取消顶部菜单
				if (!this.checked) {
					setTopMenu(this.checked);
				}
			});

			// 顶部菜单
			$('#topMenu').click(function() {
				setTopMenu(this.checked);

				// 浮动子菜单
				if (this.checked)
					setHoverSubMenu(this.checked);

				// 主题
				if (this.checked) {
					// 清除左侧主题
					setLeftSideStyle();
					// 设置头部主题
					var $currStyle = $(".set-adminheader-style.active");
					var headerStyle = $currStyle.attr("data-header-bg");
					if (!headerStyle)
						headerStyle = "bg-header-1";
					setHeaderStyle(headerStyle);
				}
			});

			// 固定宽度
			$('#boxedLayout').click(function() {
				setBoxedLayout(this.checked);
			});

			// 顶部主题
			$(".set-adminheader-style").click(function() {
				setHeaderStyle($(this).attr("data-header-bg"));

				// 清除左侧主题
				setLeftSideStyle();
			});

			// 左侧主题
			$(".set-adminleftside-style").click(function() {
				setLeftSideStyle($(this).attr("data-leftside-bg"));

				// 清除顶部主题
				setHeaderStyle();

				// 触发取消顶部菜单
				setTopMenu(false);
			});

			// 重置风格事件
			$(".reset-style").click(function() {
				resetStyle();
			});

			// 设置个性化初始值
			var compactMenu, minMenu, hoverSubMenu, boxedLayout, topMenu, headerStyle, leftSideStyle;

			if (localStorageSupport) {
				compactMenu = localStorage.getItem("compact_menu");// 简洁菜单
				minMenu = localStorage.getItem("min_menu");// 最小化菜单
				hoverSubMenu = localStorage.getItem("hover_sub_menu");// 浮动子菜单
				boxedLayout = localStorage.getItem("boxed_layout");// 固定宽度
				topMenu = localStorage.getItem("top_menu");// 顶部菜单
				headerStyle = localStorage.getItem("header_style");// 顶部主题
				leftSideStyle = localStorage.getItem("left_side_style");// 左侧主题
			}

			setStyle(compactMenu, minMenu, hoverSubMenu, boxedLayout, topMenu, headerStyle, leftSideStyle);
		},

		// 显示未读消息数
		showUnreadMsgBadge : function(count) {
			showUnreadMsgBadge(count);
		}
	}

	// 是否支持HTML5 local storage
	function localStorageSupport() {
		return (('localStorage' in window) && window['localStorage'] !== null)
	}

	// 简洁菜单
	function setCompactMenu(isCheck) {
		if (isCheck) {
			$('#sidebar').addClass('compact');
			$('#navbar').addClass('compact');
			$('#logo').addClass('compact');
		} else {
			$('#sidebar').removeClass('compact');
			$('#navbar').removeClass('compact');
			$('#logo').removeClass('compact');
			$('#sidebar[data-sidebar-scroll=true]').ace_sidebar_scroll('reset')
		}

		if (ace.vars['old_ie'])
			ace.helper.redraw($('#sidebar')[0], true);

		// 记录到本地
		if (localStorageSupport) {
			if (isCheck)
				localStorage.setItem("compact_menu", 'on');
			else
				localStorage.setItem("compact_menu", 'off');
		}

		if ($('#compactMenu'))
			$('#compactMenu').prop('checked', isCheck); // 触发按钮

		// 浮动子菜单
		if (this.checked)
			setHoverSubMenu(this.checked);

		$(window).resize(); // 手工触发
	}

	// 最小化菜单
	function setMinMenu(isCheck) {
		$sidebar = $("#sidebar");
		if ($sidebar == null || $sidebar.length == 0)
			$sidebar = $(this).closest('.sidebar');
		if ($sidebar.length == 0)
			return;

		if (isCheck) {
			if (!$('#navbar').hasClass('menu-min')) {
				$('#navbar').addClass('menu-min');
				$('#logo').addClass('menu-min');
				$sidebar.ace_sidebar('toggleMenu', this);
			}
		} else {
			if ($('#navbar').hasClass('menu-min')) {
				$('#navbar').removeClass('menu-min');
				$('#logo').removeClass('menu-min');
				$sidebar.ace_sidebar('toggleMenu', this);
			}
		}

		// 记录到本地
		if (localStorageSupport) {
			if (isCheck)
				localStorage.setItem("min_menu", 'on');
			else
				localStorage.setItem("min_menu", 'off');
		}

		if ($('#minMenu'))
			$('#minMenu').prop('checked', isCheck); // 触发按钮

		$(window).resize(); // 手工触发
	}

	// 浮动子菜单
	function setHoverSubMenu(isCheck) {
		if (isCheck) {
			$('#sidebar li').addClass('hover').filter('.open').removeClass('open').find('> .submenu').css('display',
					'none');
		} else {
			$('#sidebar li.hover').removeClass('hover');
		}

		$('.sidebar[data-sidebar-hover=true]').ace_sidebar_hover('reset')
		$('.sidebar[data-sidebar-scroll=true]').ace_sidebar_scroll('reset')

		if (ace.vars['old_ie'])
			ace.helper.redraw($('#sidebar')[0]);

		// 记录到本地
		if (localStorageSupport) {
			if (isCheck)
				localStorage.setItem("hover_sub_menu", 'on');
			else
				localStorage.setItem("hover_sub_menu", 'off');
		}

		if ($('#hoverSubMenu'))
			$('#hoverSubMenu').prop('checked', isCheck); // 触发按钮
	}

	// 固定宽度
	function setBoxedLayout(isCheck) {
		if (isCheck) {
			$("body").addClass('boxed-layout');
		} else {
			$("body").removeClass('boxed-layout');
		}

		// 记录到本地
		if (localStorageSupport) {
			if (isCheck)
				localStorage.setItem("boxed_layout", 'on');
			else
				localStorage.setItem("boxed_layout", 'off');
		}

		if ($('#boxedLayout'))
			$('#boxedLayout').prop('checked', isCheck); // 触发按钮
	}

	// 顶部菜单
	function setTopMenu(isCheck) {
		if (isCheck) {
			$('#sidebar').addClass('h-sidebar');
			$('#navbar').addClass('h-navbar');
			$('#logo').addClass('h-navbar');
		} else {
			$('#sidebar').removeClass('h-sidebar');
			$('#navbar').removeClass('h-navbar');
			$('#logo').removeClass('h-navbar');
			$('#sidebar[data-sidebar-scroll=true]').ace_sidebar_scroll('reset')
		}

		if (ace.vars['old_ie'])
			ace.helper.redraw($('#sidebar')[0], true);

		// 记录到本地
		if (localStorageSupport) {
			if (isCheck)
				localStorage.setItem("top_menu", 'on');
			else
				localStorage.setItem("top_menu", 'off');
		}

		if ($('#topMenu'))
			$('#topMenu').prop('checked', isCheck); // 触发按钮

		$(window).resize(); // 手工触发
	}

	// 切换顶部主题
	function setHeaderStyle(a) {
		// 清除左侧视角
		if (a && !a.match(/-0$/)) // 以-0结尾
			$("body").removeClass('left-side-view');

		// 主题
		$("#navbar-container").removeClass(function(a, b) {
			return (b.match(/(^|\s)bg-\S+/g) || []).join(" ")
		});
		if (a)
			$("#navbar-container").addClass(a);

		$("#navbar").removeClass(function(a, b) {
			return (b.match(/(^|\s)bg-\S+/g) || []).join(" ")
		});
		if (!$("#navbar-container").hasClass("container") && a)
			$("#navbar").addClass(a);

		// 按钮active
		$(".set-adminheader-style").removeClass("active");
		$('.set-adminheader-style').each(function() {
			if ($(this).attr("data-header-bg") == a)
				$(this).addClass("active");
		});

		// 记录到本地
		if (localStorageSupport) {
			if (a)
				localStorage.setItem("header_style", a);
			else
				localStorage.removeItem("header_style");
		}

		$(window).resize(); // 手工触发
	}

	// 切换左侧主题
	function setLeftSideStyle(a) {
		// 添加左侧视角
		if (a && !a.match(/-0$/)) // 以-0结尾
			$("body").addClass('left-side-view');

		// 主题
		$("#sidebar").removeClass(function(a, b) {
			return (b.match(/(^|\s)bg-\S+/g) || []).join(" ")
		});
		if (a)
			$("#sidebar").addClass(a);

		// 按钮active
		$(".set-adminleftside-style").removeClass("active");
		$('.set-adminleftside-style').each(function() {
			if ($(this).attr("data-leftside-bg") == a)
				$(this).addClass("active");
		});

		// 记录到本地
		if (localStorageSupport) {
			if (a)
				localStorage.setItem("left_side_style", a);
			else
				localStorage.removeItem("left_side_style");
		}
	}

	// 未读消息显示
	function showUnreadMsgBadge(count) {
		var $badge = $('.alertInfo').find(".badge");
		if (!$badge)
			return;

		$badge.text(count);

		// 是否隐藏
		if (count && parseInt(count) > 0) {
			$badge.removeClass('badge-hidden');
			$badge.addClass("flash");
			setTimeout(function() {
				$badge.removeClass('flash');
			}, 6000);
		} else {
			$badge.addClass("badge-hidden");
		}
	}

	// 重置风格
	function resetStyle() {
		if (localStorageSupport) {
			localStorage.removeItem("compact_menu");
			localStorage.removeItem("min_menu");
			localStorage.removeItem("hover_sub_menu");
			localStorage.removeItem("boxed_layout");
			localStorage.removeItem("top_menu");
			localStorage.removeItem("header_style");
			localStorage.removeItem("left_side_style");
		}
		setStyle();
	}

	function resetHeaderView() {
		if (localStorageSupport) {
			localStorage.removeItem("compact_menu");
			localStorage.removeItem("min_menu");
			localStorage.removeItem("hover_sub_menu");
			localStorage.removeItem("boxed_layout");
			localStorage.removeItem("top_menu");
			localStorage.removeItem("header_style");
			localStorage.removeItem("left_side_style");
		}
		setStyle();
	}

	// 设置风格
	function setStyle(compactMenu, minMenu, hoverSubMenu, boxedLayout, topMenu, headerStyle, leftSideStyle) {
		// 默认值
		if (!compactMenu)
			compactMenu = "off";
		if (!minMenu)
			minMenu = "off";
		if (!hoverSubMenu)
			hoverSubMenu = "off";
		if (!boxedLayout)
			boxedLayout = "off";
		if (!topMenu)
			topMenu = "off";
		if (!headerStyle && !leftSideStyle) {
			if (dts.util.isEnvProd()) {
				headerStyle = "bg-header-0";
				leftSideStyle = "bg-leftside-1";
			} else {
				headerStyle = "bg-header-1";
				leftSideStyle = "bg-leftside-0";
			}
		} else if (!headerStyle) {
			headerStyle = "bg-header-0";
		} else if (!leftSideStyle) {
			leftSideStyle = "bg-leftside-0";
		}

		var body = $('body');

		// 固定NAVBAR
		ace.settingFunction.navbar_fixed(null, true);

		// 固定SIDEBAR
		ace.settingFunction.sidebar_fixed(null, true);

		// 简洁菜单
		if (compactMenu == 'on') {
			setCompactMenu(true);
		} else {
			setCompactMenu(false);
		}

		// 最小化菜单
		if (minMenu == 'on') {
			setMinMenu(true);
		} else {
			setMinMenu(false);
		}

		// 浮动子菜单
		if (hoverSubMenu == 'on') {
			setHoverSubMenu(true);
		} else {
			setHoverSubMenu(false);
		}

		// 固定宽度
		if (boxedLayout == 'on') {
			setBoxedLayout(true);
		} else {
			setBoxedLayout(false);
		}

		// 顶部菜单
		if (topMenu == 'on') {
			setTopMenu(true);
		} else {
			setTopMenu(false);
		}

		// 顶部主题
		if (headerStyle) {
			setHeaderStyle(headerStyle);
		}

		// 左侧主题
		if (leftSideStyle) {
			setLeftSideStyle(leftSideStyle);
		}
	}

	window.myAce = myAce;

}).call(this);